<template>
  <div class="app-container">
    <!-- 按钮组 -->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" plain @click="openDrawer">新增</el-button>
        <el-button type="danger" plain @click="allDelClk">批量删除</el-button>
      </el-col>
      <el-col :span="12">
        <div>
          <el-input
            @blur="onBlurFn"
            v-model="searchIpt"
            placeholder="请输入内容"
          >
            <el-button
              slot="append"
              @click="onSearchClk"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </div>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-table
      :data="list"
      style="width: 100%; margin-top: 20px"
      max-height="400"
      ref="multipleTable"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column fixed type="selection" width="50"> </el-table-column>
      <el-table-column type="index" label="#" width="120"> </el-table-column>
      <el-table-column prop="title" label="题目" width="300"> </el-table-column>
      <el-table-column prop="author" label="作者" width="50"> </el-table-column>
      <el-table-column prop="date" sortable label="日期" width="200">
      </el-table-column>
      <el-table-column prop="type" label="类别" width="100"> </el-table-column>
      <el-table-column prop="time" label="时间" width="50"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button @click="openDrawer(scope.row)" type="text" size="small">
            编辑
          </el-button>
          <el-button @click="onLookClk(scope.row)" type="text" size="small">
            查看
          </el-button>
          <el-button @click="deleteRow(scope.row.id)" type="text" size="small">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 抽屉 -->
    <el-drawer size="70%" :title="title" :visible.sync="drawer" direction="rtl">
      <el-form
        ref="form"
        style="padding-right: 10%"
        :model="form"
        label-width="80px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题">
              <el-input v-model="form.title" :disabled="isView"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="作者">
              <el-input disabled v-model="form.author"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别">
              <el-select
                v-model="form.cid"
                :disabled="isView"
                placeholder="请选择类别"
              >
                <el-option
                  v-for="(v, i) in dealClassifyList"
                  :value="v.id"
                  :label="v.name"
                  :key="i"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用时">
              <el-select
                v-model="form.time"
                :disabled="isView"
                placeholder="请选择用时"
              >
                <el-option label="一分钟" value="1"></el-option>
                <el-option label="二分钟" value="2"></el-option>
                <el-option label="三分钟" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="内容">
          <mavon-editor
            name
            style="max-height: 300px; overflow-y: auto"
            v-model="form.content"
          ></mavon-editor>
          <div class="zzc" v-if="isView"></div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-if="!isView" @click="closeDrewerFn">
            {{ title }}
          </el-button>
          <el-button @click="onCancelClk">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 5, 8, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
import { mapActions, mapMutations, mapState } from "vuex";
export default {
  data() {
    return {
      drawer: false,
      title: "新增",
      form: {
        author: "situ",
      },
      isView: false,
      ids: "",
      searchIpt: "",
    };
  },
  methods: {
    ...mapActions("msbd", ["getlist", "addlist", "updatelist", "deletelist"]),
    ...mapMutations("msbd", [
      "SET_PAGESIZE",
      "SET_CURRENTPAGE",
      "SET_SEARCHVAL",
      "SET_ORDER_SORT",
    ]),
    sortChange(val) {
      this.SET_ORDER_SORT(val);
    },
    async onSearchClk() {
      if (!this.searchIpt.trim()) {
        this.$message({
          type: "warning",
          message: "请输入您要搜索的内容",
        });
        return;
      }
      await this.SET_SEARCHVAL(this.searchIpt);
      this.getlist();
    },
    onBlurFn() {
      if (!this.searchIpt.trim()) {
        this.SET_SEARCHVAL("");
        this.getlist();
      }
    },
    handleSizeChange(val) {
      this.SET_PAGESIZE(val);
      this.getlist();
    },
    handleCurrentChange(val) {
      this.SET_CURRENTPAGE(val);
      this.getlist();
    },
    // 单删
    deleteRow(id) {
      this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          id = id + "";
          this.deletelist(id);

          // let ids = id.split(",");
          // if (ids.length == this.tableData.length && this.currentPage != 1) {
          //   this.SET_CURRENTPAGE(this.currentPage - 1);
          // }

          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
          this.$refs.multipleTable.clearSelection();
        });
    },
    // 多删
    allDelClk() {
      if (!this.ids.trim()) {
        this.$message("请选择您要删除的项!!!");
        return;
      }
      this.deleteClk(this.ids);
    },
    // 复选框的点击事件
    handleSelectionChange(val) {
      let arr = [];
      for (let i = 0; i < val.length; i++) {
        arr.push(val[i].id);
      }

      this.ids = arr.join(",");
    },
    // 新增 || 修改
    openDrawer(e) {
      if (e.id) {
        this.title = "修改";
        let obj = { ...e };
        this.form = obj;
      } else {
        this.title = "新增";
      }
      this.isView = false;
      this.drawer = true;
    },
    // 点击 确定 按钮 关闭 抽屉
    async closeDrewerFn() {
      if (this.form.id) {
        // 修改
        await this.updatelist(this.form);
      } else {
        // 新增
        await this.addlist(this.form);
      }

      this.onCancelClk();
    },
    // 查看
    onLookClk(row) {
      this.form = row;
      this.isView = true;
      this.title = "查看";
      this.drawer = true;
    },
    // 取消
    onCancelClk() {
      this.drawer = false;
      this.form = { author: "situ" };
    },
  },
  created() {
    this.getlist();
    this.$store.dispatch("classify/getCid");
  },
  computed: {
    ...mapState("msbd", ["list", "total", "currentPage", "pageSize"]),
    ...mapState("classify", ["classifyList"]),
    dealClassifyList() {
      return this.classifyList.filter((v) => v.id);
    },
  },
  components: { mavonEditor },
};
</script>

<style scoped>
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>

<style>
#el-drawer__title > span {
  outline: none;
}

.zzc {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(245, 247, 250, 0.5);
  border-color: #e4e7ed;
  cursor: no-drop;
  z-index: 99999;
}

.el-table .warning-row {
  background: oldlace;
}
</style>